x = int(input())
ma = 0
if len(str(x)) == 4:
    for i in range(2, x + 1):
        if str(i) in str(x):
            t = False
            for dl in range(2, i // 2 + 1):
                if i % dl == 0:
                    t = True
                    break
            if not t:
                ma = max(ma, i)
print(ma)
